package com.mes.loco.aps.server.controller.mrp;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.mrp.MRPMaterialPlan;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料需求计划控制器
 *
 * @author YouWang·Peng
 * 2022-3-10 13:32:09
 */
@RestController
@RequestMapping("/api/MRPMaterialPlan")
public class MRPMaterialPlanController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MRPMaterialPlanController.class);

    final
    APSService wAPSService;

    final MyHelperService wMyHelperService;

    public MRPMaterialPlanController(APSService wAPSService, MyHelperService wMyHelperService) {
        this.wAPSService = wAPSService;
        this.wMyHelperService = wMyHelperService;
    }

    /**
     * 条件查询配料需求计划
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (!CoreServiceImpl.getInstance()
                    .BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 52013141, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wStepID = StringUtils.parseInt(request.getParameter("StepID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            int wMaterialType = StringUtils.parseInt(request.getParameter("MaterialType"));
            int wReplaceType = StringUtils.parseInt(request.getParameter("ReplaceType"));
            int wOutSourceType = StringUtils.parseInt(request.getParameter("OutSourceType"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<MRPMaterialPlan>> wServiceResult = wAPSService.MRP_QueryMaterialPlanList(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderID, wPartID, wStepID, wMaterial, wMaterialType,
                    wReplaceType, wOutSourceType, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 手动触发物料配送单
     */
    @GetMapping("/HandTrigger")
    public Object HandTrigger(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (!CoreServiceImpl.getInstance()
                    .BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID, 52013142, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));

            if (wOrderID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.MRP_HandTrigger(wLoginUser, wOrderID, wPartID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 手动下达周计划触发物料需求计划
     */
    @GetMapping("/IssueWeekPlan")
    public Object IssueWeekPlan(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wWeekPlanID = StringUtils.parseInt(request.getParameter("WeekPlanID"));

            if (wWeekPlanID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wAPSService.APS_IssueWeekPlan(wLoginUser,
                    wWeekPlanID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 根据委外件的物料需求计划，生成这部分的领料单
     */
    @PostMapping("/TriggerPickDemandByPrevAndDisa")
    public Object TriggerPickDemandByPrevAndDisa(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<Integer> wPartIDList = CloneTool.CloneArray(wParam.get("PartIDList"), Integer.class);
            List<Integer> wMaterialIDList = CloneTool.CloneArray(wParam.get("MaterialIDList"), Integer.class);
            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));

            ServiceResult<Integer> wServiceResult = wMyHelperService.WMS_TriggerPickDemandByPrevAndDisa(wLoginUser,
                    wOrderID, wPartIDList, wMaterialIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
